<?php

require_once("../models/config.php");
require_once("../ui.php");
include_once("plot.graph.php");

if(!isUserLoggedIn()) {
	header ("location: login.php");
	die();
}

global $loggedInUser;
global $db;

function createSQLCommand() {

	$sql = "SELECT Debugging_Time AS DebuggingTime, Flag_Memory_Corruption_Related AS MemoryCorruptionFlag 
			FROM `debugging_Reports` JOIN `userCake_Users` ON `debugging_Reports`.`User` = `userCake_Users`.`User_ID` 
			JOIN root_Cause_Flags ON `debugging_Reports`.Id = root_Cause_Flags.report_Id
			WHERE (`userCake_Users`.`Group_Id` = " . STANDART_USER_GROUP_ID . ")";
		
	return $sql;
}

$sql = createSQLCommand();
$result = $db->sql_query($sql);
if (!$result) {
	$error = $db->sql_error();
	return;
}

$rows = $db->sql_fetchall();

$memoryCorruptionFlags = array(0, 1);
$flagIndexes = array();
$debuggingTimes = array();

foreach($rows as $i => $row) {
	$debuggingTime = round($row["DebuggingTime"] / 60, 2);
	$memoryCorruptionFlag =  $row["MemoryCorruptionFlag"];
	
	$flagIndex = array_search($memoryCorruptionFlag, $memoryCorruptionFlags);
	
	$flagIndexes[] = $flagIndex;
	$debuggingTimes[] = $debuggingTime; 
}

drawMyPlotGraph($debuggingTimes, $flagIndexes, $memoryCorruptionFlags, "Corruption memory occured (1 = occured)",
		"Debugging time [hours]", "How memory corruption affects debugging time",
		array("DrawMedians" => TRUE));

